package com.example.dict.ativities;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dict.R;
import com.example.dict.bean.ChengyuBean;
import com.example.dict.db.DBManager;
import com.example.dict.utils.ApiUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class ChengyuInfoActivity extends BaseActivity{

    //标志位，表示该成语是否被收藏
    boolean isCollect = false;
    boolean isExist = false;//判断成语是否已经存在

    TextView ziTv1,ziTv2,ziTv3,ziTv4,pyTv,lijuTv,fromTv;
    GridView tyGv,fyGv;
    ListView mJbsyLv,mXXsyLv;
    ImageView mCyCollectIv;
    private String chengyu;
     List<String> mJbsyList,mXxsyList,mTyList,mFyList;
    private ArrayAdapter<String> tyAdapter,fyApapter,jbsyAdapter,xxsyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chengyu_info);

        initView();
        initApaper();
//        接受上一个页面传过来的值
        Intent intent = getIntent();
        chengyu = intent.getStringExtra("chengyu");
        String Url = ApiUtil.getChengyuurl(chengyu);
        loadData(Url);
        //判断是否已经收藏的方法
        isExist = DBManager.isExistInCOLLCHENGYU(chengyu);
        isCollect = isExist;//记录初始状态
        setCollectTvStyle();
    }

    /*根据收藏的状态，改变图片颜色*/
    private void setCollectTvStyle() {
        if (isCollect) {
            mCyCollectIv.setImageResource(R.mipmap.ic_collection_fs);
        }else {
            mCyCollectIv.setImageResource(R.mipmap.ic_collection);
        }
    }

    private void initApaper() {
        mTyList = new ArrayList<>();
        mFyList = new ArrayList<>();
        mJbsyList = new ArrayList<>();
        mXxsyList = new ArrayList<>();

        tyAdapter= new ArrayAdapter<String>(this, R.layout.item_word_jslv, R.id.item_wordlv_tv, mTyList);
        fyApapter= new ArrayAdapter<String>(this, R.layout.item_word_jslv, R.id.item_wordlv_tv, mFyList);
        tyGv.setAdapter(tyAdapter);
        fyGv.setAdapter(fyApapter);
        jbsyAdapter = new ArrayAdapter<String>(this, R.layout.item_word_jslv, R.id.item_wordlv_tv, mJbsyList);
        xxsyAdapter = new ArrayAdapter<String>(this, R.layout.item_word_jslv, R.id.item_wordlv_tv, mXxsyList);
        mJbsyLv.setAdapter(jbsyAdapter);
        mXXsyLv.setAdapter(xxsyAdapter);
    }

    /*网络请求成功时调用的方法*/
    @Override
    public void onSuccess(String result) {
        ChengyuBean bean = new Gson().fromJson(result, ChengyuBean.class);
        ChengyuBean.ResultBean cybean = bean.getResult();
        if (cybean !=null) {
            //插入到数据库中
            DBManager.insertCyToCHENGYU(cybean);
            //显示数据
            showDataToView(cybean);
        }else {
            Toast.makeText(this,"成语查不到哦，换个吧~",Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 网络加载数据失败时调用此方法
     * @param ex
     * @param isOnCallback
     */
    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
       //获取数据库当中的数据
        ChengyuBean.ResultBean bean = DBManager.queryCyFromCHENGYU(chengyu);
        Log.i("animee","onError: bean = "+bean);
        if (bean != null){
            showDataToView(bean);
        }
    }


    /**
     * 获取到的数据显示在view
     * @param cybean
     */
    private void showDataToView(ChengyuBean.ResultBean cybean) {
        ziTv1.setText(String.valueOf(cybean.getName().charAt(0)));
        ziTv2.setText(String.valueOf(cybean.getName().charAt(1)));
        ziTv3.setText(String.valueOf(cybean.getName().charAt(2)));
        ziTv4.setText(String.valueOf(cybean.getName().charAt(3)));
        pyTv.setText("拼音:"+cybean.getPinyin());
        if (cybean.getLiju() != null) {
            lijuTv.setText("例句:"+cybean.getLiju());
        }else {
            lijuTv.setText("");
        }
        fromTv.setText(cybean.getChuchu());

        List<String> jList = cybean.getJyc();
        //判断是否有近义词
        if (jList != null && jList.size() !=0) {
            mTyList.addAll(jList);
            tyAdapter.notifyDataSetChanged();
        }
        List<String> fList = cybean.getFyc();
        //判断是否有反义词
        if (fList != null && fList.size() !=0) {
            mFyList.addAll(fList);
            fyApapter.notifyDataSetChanged();
        }
        List<String> jijieList = cybean.getJbsy();
        //判断是否有基本释义
        if (jijieList != null && jijieList.size() !=0) {
            mJbsyList.addAll(jijieList);
            jbsyAdapter.notifyDataSetChanged();
        }
        List<String> xxjs = cybean.getXxsy();

        //判断是否有详细释义
        if (xxjs != null && xxjs.size() !=0) {
            String str = DBManager.listToString(xxjs);

            String xj = str.replace("|", "\n").replace("<br>","\n");
//            Log.i("annimee","onSuccess:xj="+xj);
            List<String> xjList = DBManager.stringToList(xj);
            mXxsyList.addAll(xjList);
            xxsyAdapter.notifyDataSetChanged();
        }
    }


        /**
         * 初始化数据
         */
    private void initView() {
        mCyCollectIv = findViewById(R.id.cyinfo_iv_collection);
        ziTv1 = findViewById(R.id.chengyu_tv_zi1);
        ziTv2 = findViewById(R.id.chengyu_tv_zi2);
        ziTv3 = findViewById(R.id.chengyu_tv_zi3);
        ziTv4 = findViewById(R.id.chengyu_tv_zi4);
        pyTv = findViewById(R.id.chengyu_tv_py);
        lijuTv = findViewById(R.id.chengyu_tv_liju);
        fromTv = findViewById(R.id.cyinfo_tv_from);
        tyGv = findViewById(R.id.cyinfo_gv_tongyi);
        fyGv = findViewById(R.id.cyinfo_gv_fanyi);
        mJbsyLv = findViewById(R.id.cyinfo_lv_jbsy);
        mXXsyLv = findViewById(R.id.cyinfo_lv_xxsy);

    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.cyInfo_iv_back:
                finish();
                break;
            case R.id.cyinfo_iv_collection:
                isCollect = !isCollect;
                setCollectTvStyle();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isExist && !isCollect) {
            DBManager.deleteCyFromCOLLCHENGYU(chengyu);
        }
        if (!isExist && isCollect) {
            DBManager.insertCyToCOLLCHENGYU(chengyu);
        }
    }
}
